package it.unipr.ce.dsg.s2p.example.peerdroid;



import android.app.Activity;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import it.unipr.ce.dsg.s2p.example.peerdroid.R;

public class My_config_peer extends Activity implements OnClickListener{
	
	private TextView addressPeer,currentBootstrapTabConfig;
	private EditText addressBootstrapPeer,edtServerTabConfig;
	private Button saveTabConfig, closeTabConfig;
	private CheckBox checkbox;
	private String checkReach;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.my_config_peer);
		checkReach="yes";
		
		addressPeer = (TextView)findViewById(R.id.txtMyIPTabconfig);
		currentBootstrapTabConfig=(TextView)findViewById(R.id.txtCurrentBootstrapTabConfig);
		addressBootstrapPeer= (EditText)findViewById(R.id.edtBootstapTabConfig);
		edtServerTabConfig = (EditText)findViewById(R.id.edtServerTabConfig);
		saveTabConfig =(Button) findViewById(R.id.btnSaveTabConfig);
		closeTabConfig=(Button)findViewById(R.id.btnCloseTabConfig);
		
		//control checkbox
		 checkbox = (CheckBox) findViewById(R.id.reachCheckBox);
//		 checkbox.setChecked(true);//default is check, set by layout already android:checked="true"
		 checkbox.setOnClickListener(new OnClickListener() {
		     public void onClick(View v) {
		         // Perform action on clicks, depending on whether it's now checked
		         if (((CheckBox) v).isChecked()) {
		        	 checkReach="yes";
		         } else {
		        	 checkReach="no";
		         }
		     }
		 });//end checkbox
		
		if(PeerActivity.peer!=null){
			addressPeer.setText(PeerActivity.peer.getAddressPeer().toString());//display IP
			
			if(PeerActivity.peer.getBootstrapPeer()!=null){
				currentBootstrapTabConfig.setText(PeerActivity.peer.getBootstrapPeer().getURL());
			}
			else{
				currentBootstrapTabConfig.setText("None");
			}
						
			saveTabConfig.setOnClickListener(this);
			closeTabConfig.setOnClickListener(this);
		}		
	}//end onCreate
	
    public void onClick(View v){
        if(v instanceof Button){
             switch(v.getId()){
             case R.id.btnSaveTabConfig:{
                  //do something
            	 saveAction();
//            	 finish();
                  break;
             }
             case R.id.btnCloseTabConfig:{
            	//do something
//            	 PeerActivity.peer.halt();
            	 finish();
                  break;
             }
             
             }//end switch
        }// end if -instance of Button
        
       }//end onClick
    
	private void saveAction(){
		String addBootstrap = (String)addressBootstrapPeer.getText().toString();
		String server =(String)edtServerTabConfig.getText().toString();
 		if(addBootstrap.contentEquals("bootstrap@ipaddress:port")){
			Toast toast = Toast.makeText(getApplicationContext(),"Please type a BootstrapPeer (ex. bootstrap@192.168.1.2:5080)" ,Toast.LENGTH_LONG);
		 		toast.show();		 		
		}
		else{
//			Configuration bootstrap here
			//PeerActivity.peer.setConfiguration(sbc, bootstrap, reachability)
			
			PeerActivity.peer.setConfiguration(server,addBootstrap,checkReach);
			currentBootstrapTabConfig.setText(addBootstrap);//just in case user put home then come back application
		}
	}//end saveAction
	
}